package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author atguigu-mqx
 */
@RestController
@RequestMapping("/admin/product")
public class SpuManageController {

    @Autowired
    private ManageService manageService;

    //  /admin/product/{page}/{limit}
    //  http://localhost/admin/product/1/10?category3Id=61
    //  spring mvc 对象传值：只要实体类的属性名与传递的参数名称一样，则会自动映射！
    @GetMapping("{page}/{limit}")
    public Result spuList(@PathVariable Long page ,
                          @PathVariable Long limit,
                          SpuInfo spuInfo){

        //  参数需要： 分页条件 --> Page 对象中！
        Page<SpuInfo> spuInfoPage = new Page<>(page,limit);

        //  调用服务层方法
        IPage iPage = manageService.getSpuList(spuInfoPage,spuInfo);

        //  返回数据
        return Result.ok(iPage);

    }

    //  /admin/product/baseSaleAttrList
    @GetMapping("baseSaleAttrList")
    public Result getBaseSaleAttrList(){
        //  调用服务层方法
        List<BaseSaleAttr> baseSaleAttrList = this.manageService.getBaseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //  /admin/product/saveSpuInfo
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        //  调用服务层方法
        this.manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //  获取spuInfo 的控制器！
    @GetMapping("getSpuInfo/{spuId}")
    public Result getSpuInfo(@PathVariable Long spuId) {
        SpuInfo spuInfo = manageService.getFullSpuInfo(spuId);
        return Result.ok(spuInfo);
    }

    //  更新spuInfo 的控制器
    @PostMapping("updateSpuInfo")
    public Result updateSpuInfo(@RequestBody SpuInfo spuInfo){
        // 调用服务层的保存方法
        manageService.updateSpuInfo(spuInfo);
        return Result.ok();
    }

    //  /admin/product/spuImageList/{spuId}
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
        //  调用服务层方法
        List<SpuImage> spuImageList = this.manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //  /admin/product/spuSaleAttrList/{spuId}
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId){
        //  调用服务层方法！
        List<SpuSaleAttr> spuSaleAttrList =  this.manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
}
